﻿#include "PLCReadThread.h"
#include "PLCCommOPCUA.h"
#include <QTime>

const QStringList s_otherDataID_OnlyRead = {
    u8"omron.nj101.腔外壳体温度", u8"omron.nj101.压力实数9", u8"omron.nj101.水温1",
    u8"omron.nj101.水温2", u8"omron.nj101.水压", u8"omron.nj101.水流",
    u8"omron.nj101.Loop1PV", u8"omron.nj101.Loop2PV"
};

const QStringList s_temperatureID_OnlyRead = {
    u8"omron.nj101.实际温度1", u8"omron.nj101.实际温度2"
};

const QStringList s_MKSID_OnlyRead = {
    u8"omron.nj101.MKS反馈1", u8"omron.nj101.MKS反馈2"
};

const QStringList s_pressuremeterID_OnlyRead = {
    u8"omron.nj101.压力读数1", u8"omron.nj101.压力读数2"
};

const QStringList s_otherValveID_OnlyRead = {
    u8"omron.nj101.规阀", u8"omron.nj101.新增真空泵", u8"omron.nj101.新增真空泵_MP启动",
    u8"omron.nj101.新增真空泵_BP启动", u8"omron.nj101.炉子开启", u8"omron.nj101.指示灯1",
    u8"omron.nj101.指示灯", u8"omron.nj101.指示灯2"
};

const QStringList s_flowmeterID_OnlyRead = {
    u8"omron.nj101.流量读数1", u8"omron.nj101.流量读数2", u8"omron.nj101.流量读数3",
    u8"omron.nj101.流量读数4", u8"omron.nj101.流量读数5", u8"omron.nj101.流量读数6",
    u8"omron.nj101.流量读数7", u8"omron.nj101.流量读数8", u8"omron.nj101.流量读数9"
};

const QStringList s_valveID_OnlyRead = {
    u8"omron.nj101.阀门输出1", u8"omron.nj101.阀门输出2", u8"omron.nj101.阀门输出3",
    u8"omron.nj101.阀门输出4", u8"omron.nj101.阀门输出5", u8"omron.nj101.阀门输出6",
    u8"omron.nj101.阀门输出7", u8"omron.nj101.阀门输出8", u8"omron.nj101.阀门输出9",
    u8"omron.nj101.阀门输出10", u8"omron.nj101.阀11"
};

PLCReadThread::PLCReadThread(Open62541 *client, PLCCommOPCUA *parent)
    : m_pParent{parent}
{
    m_client = client;
}

void PLCReadThread::stop()
{
    m_isRuning = false;
}

void PLCReadThread::run()
{
    QMap<QString, QVariant> data;
    bool success;
    QTime t = QTime::currentTime();

    m_isRuning = true;
    while(m_isRuning)
    {
        //读取所有的数据，把读取的数据都放到 parent（PLCCommOPCUA） 里。

        //读所有电磁阀值
        success = m_client->Open62541_batchReading(2, s_valveID_OnlyRead, data);
        if (success)
        {
            for (int i = 0; i < s_valveID_OnlyRead.size(); ++i)
            {
                m_valveStatus[i] = data.value(s_valveID_OnlyRead[i]).toBool();
            }
        }

        //读所有流量计
        bool success = m_client->Open62541_batchReading(2, s_flowmeterID_OnlyRead, data);
        if (success) {
            for (int i = 0; i < s_flowmeterID_OnlyRead.size(); ++i)
            {
                m_flowMeter[i] = data.value(s_flowmeterID_OnlyRead[i]).toInt();
            }
        }


        //读所有压力值
        success = m_client->Open62541_batchReading(2, s_pressuremeterID_OnlyRead, data);
        if (success)
        {
            for (int i = 0; i < s_pressuremeterID_OnlyRead.size(); ++i)
            {
                m_pressureMeter[i] = data.value(s_pressuremeterID_OnlyRead[i]).toInt();
            }
        }

        // 读 2 个温度
        success = m_client->Open62541_ReadData(2, s_temperatureID_OnlyRead[0], m_temperature[0]) &&
                       m_client->Open62541_ReadData(2, s_temperatureID_OnlyRead[1], m_temperature[1]);


        /// TODO 还有其他需要读的，都在这里完成
        ///
        ///

        // 完成读取后要把数据拷贝给 PLCCommOPCUA, 这里需要加锁，因为主界面可能正在从 PLCCommOPCUA 里面读取数据
        m_pParent->m_mutex.lock();

        // TODO 这里需要做变换，把读到的 int16 转化为物理单位
        for(int i = 0; i < 11; i++)
        {
            m_pParent->m_valveStatus[i] = m_valveStatus[i];
        }

        for(int i = 0; i < 9; i++)
        {
            m_pParent->m_flowMeter[i] = m_flowMeter[i];
        }

        for(int i = 0; i < 11; i++)
        {
            m_pParent->m_pressureMeter[i] = m_pressureMeter[i];
        }

        m_pParent->m_temperature[0] = m_temperature[0];
        m_pParent->m_temperature[1] = m_temperature[1];

        for(int i = 0; i < 8; i++)
        {
            m_pParent->m_otherValveStatus[i] = m_otherValveStatus[i];
        }

        for(int i = 0; i < 8; i++)
        {
            m_pParent->m_otherData[i] = m_otherData[i];
        }

        m_pParent->m_mutex.unlock();

        // 之后等待到下次读取时间到来
        t.addMSecs(m_interval_msecs); //
        QTime ct = QTime::currentTime();
        int msecs = ct.msecsTo(t);
        t = ct;
        QThread::msleep(msecs);
    }
}
